﻿//var _AbsolutePath = "http://localhost:19771/";
$(document).ready(function () {   
    var isMobile = window.matchMedia("(max-width: 480px)");
    if (isMobile.matches) $(".loginError.hide.desktop").remove();
    else $(".loginError.hide.mobile").remove();

    $("#submit").attr('disabled', 'disabled');
    $("#accessCode").focus();
    $("#txtName,#txtEmail,#txtMessage").keyup(function () {
        if ($("#txtName").val() != "" && $("#txtEmail").val() != "" && $("#txtMessage").val() != "") {
            $("#submit").removeAttr('disabled');
        } else {
            $("#submit").attr('disabled', 'disabled');
        }
    });

    $("#logIn").click(function () {
        if ($("#accessCode").val()) {
            if ($("#accessCode").val().split("-").length === 3 && $("#accessCode").val().split("-")[0].toUpperCase() == $("#clinicCode").val()) {
                $(".loginError").addClass("hide");
                $.ajax({
                    type: "POST",
                    url: _AbsolutePath + 'User/ValidateUser',
                    contentType: "application/json",
                    data: JSON.stringify({ accessCode: $("#accessCode").val() }),
                    caller: arguments.callee.name,//gets currently running function name
                    dataType: "json", //Cross domain call (even json also work)
                    success: function (res) {
                        if (res.Id > 0) {
                            if (res.Detail === "D")
                                location.href = "/Appointment/AppoinmentIndex";
                            else
                                location.href = "/Patient/Index";
                        }
                        else {
                            $(".loginError").removeClass("hide");
                        }
                    },
                    error: function (xhr) { debugger; }
                });
            }
            else {
                $(".loginError").removeClass("hide");
            }
        }
    });

    $("#submit").click(function () {
        $.ajax({
            type: "POST",
            url: _AbsolutePath + 'User/SendEmail',
            contentType: "application/json",
            data: JSON.stringify({ name: $("#txtName").val(), from: $("#txtEmail").val(), message: $("#txtMessage").val() }),
            caller: arguments.callee.name,//gets currently running function name
            dataType: "json", //Cross domain call (even json also work)
            success: function (res) {
                if (res == true)
                    alert("Mail sent successfully");
                else
                    alert("Unable to send message");
            },
            error: function (xhr) {
                alert("Unable to send message");
            }
        });
    });
});